<template>
<div>
  <el-button type="primary" plain @click="dialogVisible = true;"
             style="position: absolute;left: 330px;top: 80px;">添加</el-button>
  <el-dialog
    title="提示"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="handleClose">
    <el-input placeholder="请输入纸料" v-model="texture.name" style="width: 300px"></el-input>
    <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false;add()">确 定</el-button>
  </span>
  </el-dialog>
  <div>
    <br>
    <br>
    <el-table
      :data="tableData"
      stripe
      style="width: 100%">
      <el-table-column
        prop="id"
        label="编号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="纸质类型"
        width="180">
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-edit"
            @click="dialogFormVisible=true;textures=scope.row"></el-button>
          <el-button
            size="mini"
            type="danger" icon="el-icon-delete"
            @click="del(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      small
      layout="total,prev, pager, next"
      :page-size="pageSize"
      :total="total" @current-change="page">
    </el-pagination>

    <el-dialog title="纸质修改" :visible.sync="dialogFormVisible">
      <el-form :model="textures">
        <el-form-item label="编号">
          <el-input v-model="textures.id" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="名称" >
          <el-input v-model="textures.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update()">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</div>
</template>

<script>
export default {
  name: "Role",

  data(){
    return  {
      texture:{name:""},
      textures:{},
      total: 0,
      pageSize: 6,
      tableData: [],
      dialogFormVisible: false,
      dialogVisible: false
    };

  },
  //初始化
  created() {
    this.find()
  },
  methods:{
    find(){
      this.$http.post("/backservice/texture/findpage?pageNum=0&pageSize=" + this.pageSize).then(response => {
        console.log(response.userList)
        this.tableData = response.userList
        this.total = response.TotalNum
      })
    },
    del(row){
      var f=new FormData()
      f.append("id",row.id)
      this.$http.post("/backservice/texture/delete",{id:row.id}).then(data=>{
        if (data>0){
          this.find()
          alert("删除成功")
        }else {
          alert("删除失败")
        }
      })
    },
    add(){
      this.$http.post("/backservice/texture/add",this.texture).then(add=>{
        if (add>0){
          this.find()
          alert("添加成功")
        }else {
          alert("添加失败")
        }
      })
    },
    update(){
     this.dialogFormVisible = false
      this.$http.post("/backservice/texture/update",this.textures).then(update=>{
        alert(update)
        if (update>0){
          this.find()
          alert("修改成功")
        }else {
          alert("修改失败")
        }
      })
    },
    page(currentPage) {
     this.$http.post('/backservice/texture/findpage?&pageNum=' + (currentPage - 1) + '&pageSize=' + this.pageSize)
        .then(response => {
          this.tableData = response.userList
        })
    },
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done();
        })
        .catch(_ => {});
    }

  }

}
</script>

<style scoped>

</style>
